<template>
	<view style="padding:35px;">
		<uinput v-model="phone" placeholder="请输入手机号" type="tel" labelPosition="top" label="手机号"></uinput>
		<uinput v-model="password" placeholder="请输入密码" type="password" labelPosition="top" label="密码"></uinput>
		<button type="default" @click="login()">登录</button>
		<view class="url" style="margin-top: 20rpx;">
			<navigator style="float: left;" url="/pages/reset/reset" open-type="navigate">
				<view class="uni-link-item">忘记密码</view>
			</navigator>
			<navigator style="float: right;" url="/pages/join/join" open-type="navigate">
				<view class="uni-link-item">注册账号</view>
			</navigator>
		</view>
	</view>
</template>
<script>
	import uinput from "../../components/poiuy-uinput/uinput.vue"
	export default {
		components: {
			uinput
		},
		name: "login",
		data() {
			return {
				phone: "",
				password: "",
			};
		},
		onLoad: function() {
			this.phone = "";
			this.password = "";
		},
		methods: {
			login() {
				uni.removeStorage({
					key: 'token',
					success: function(res) {}
				});
				var t = this;
				uni.request({
					url: 'http://localhost:4567/user_json/login.opera',
					method: "POST",
					data: {
						phone: this.phone,
						password: this.password
					},
					success: (res) => {
						var data = res.data;
						console.log(data)
						if (res.data.code) {
							uni.showModal({
								title: '提示',
								content: res.data.msg,
								success: function(res) {
									if (res.confirm) {
										uni.navigateBack();
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						} else {
							uni.setStorageSync('token', data);
							uni.showModal({
								title: '提示',
								content: '登录成功，前往主页',
								success: function(res) {
									if (res.confirm) {
										uni.navigateBack();
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
							setTimeout(function() {
								uni.navigateBack();
							}, 2000);
						}
					},
					fail: (res) => {}
				});
			}
		},
	}
</script>
<style lang="less" scoped>
</style>
